#include <signal.h>

#include "NewThing.h"

int doPhysics_thread(void* p_data)
{
	const double day_in_seconds = (24.0*60.0*60.0);

	Space* space = ((NewThing*)p_data)->getSpace();

	unsigned int oldnow = mtime();

	while(((NewThing*)p_data)->running())
	{
		SDL_Delay(10); // dunno optimal value

		unsigned int now = mtime();
	
		double realtime_passed_between_updates = (now-oldnow)/1000.0;

		space->doPhysics(10 * day_in_seconds * realtime_passed_between_updates);
	
		oldnow = now;
	}
	
	return 0;
}

NewThing thing;

void int_handler(int p_sig)
{
	switch(p_sig)
	{
		case SIGINT:
			thing.stop();
			break;
		default:
			break;
	}
}

int main(int argc, char *argv[])
{
	signal(SIGINT, int_handler);

	SDL_Thread* thread = SDL_CreateThread(doPhysics_thread, (void*)&thing);

	thing.Run();

	int return_val;
	SDL_WaitThread(thread, &return_val);

	return 0;
}

